package life.kenny.community.provider;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.demo.BucketRefererDemo;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

import static com.qcloud.cos.demo.BucketRefererDemo.cosClient;


/**
 * @author Jay
 * @date 2021/12/13 9:41 上午
 */
@Service
public class QCloudProvider {

    @Value("${qcloud.secret.id}")
    private String secretId;
    @Value("${qcloud.secret.key}")
    private String secretKey;

    public PutObjectResult upload(MultipartFile originFile) {

        // 1 初始化用户身份信息（secretId, secretKey）。
        // SECRETID和SECRETKEY请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region("ap-shanghai");
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        // 从 5.6.54 版本开始，默认使用了 https
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
//        COSClient cosClient = new COSClient(cred, clientConfig);
        COSClient cosClient = new COSClient(cred, clientConfig);

       /*
       String bucket = "community-1303181145"; //存储桶名称，格式：BucketName-APPID
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucket);
        // 设置 bucket 的权限为 Private(私有读写)、其他可选有 PublicRead（公有读私有写）、PublicReadWrite（公有读写）
        createBucketRequest.setCannedAcl(CannedAccessControlList.Private);
        try{
            Bucket bucketResult = BucketRefererDemo.cosClient.createBucket(createBucketRequest);
        } catch (CosServiceException serverException) {
            serverException.printStackTrace();
        } catch (CosClientException clientException) {
            clientException.printStackTrace();
        }
        */

        //
//        File localFile = new File(localFilePath);
        // 指定文件将要存放的存储桶
        String bucketName = "community-1303181145";
        // 指定文件上传到 COS 上的路径，即对象键。例如对象键为folder/picture.jpg，则表示将文件 picture.jpg 上传到 folder 路径下
        String key = "images/"+originFile.getOriginalFilename();
        //指定要上传的文件,spring里一般都是MultipartFile，这里我们与官方文档推荐的方法不一样，多了一个ObjectMetadata
//        File localFile = new File(originFile.getOriginalFilename());
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLanguage(String.valueOf(originFile.getSize()));
        objectMetadata.setContentType(originFile.getContentType());
        InputStream ins = null;
        try {
             ins = originFile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key,ins,objectMetadata);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        //PutObjectResult putObjectResult = BucketRefererDemo.cosClient.putObject(putObjectRequest);

        return putObjectResult;
    }



}
